package com.lcn.netty.handler;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

public class HeartBeatHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        // 判断evt是否是IdleStatEvent(空闲事件状态)
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            if (event.state() == IdleState.READER_IDLE) {
//                System.out.println("进入读空闲");
            } else if (event.state() == IdleState.WRITER_IDLE) {
//                System.out.println("进入写空闲");
            } else if (event.state() == IdleState.ALL_IDLE) {
                System.out.println("channel关闭前，clients数量为" + ChatHandler.clients.size());
                Channel channel = ctx.channel();
                channel.close();
                System.out.println("channel关闭前，clients数量为" + ChatHandler.clients.size());
            }
        }
    }

}
